package com.liyao.storehouse.service.impl;

import com.liyao.storehouse.dao.userDao;
import com.liyao.storehouse.pojo.menu.MenuTwoList;
import com.liyao.storehouse.pojo.menu.MenuType;
import com.liyao.storehouse.pojo.users.Role;
import com.liyao.storehouse.pojo.users.user;
import com.liyao.storehouse.service.userService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: storehouse
 * @description:
 * @author: liu yan
 * @create: 2024-11-18 15:11
 */
@Service
public class userServiceImpl implements userService {
    @Resource
    userDao dao;
    @Override
    public JSONObject getUser(int page, int limit, HttpSession session) {
        JSONObject js=new JSONObject();
        JSONArray arr=new JSONArray();
        List<user> list;
        int x= 0;
        if(session.getAttribute("role_id").toString().equals("1")){
            list=dao.gerUsers((page-1)*limit,limit);
            x=dao.getUserCount();
        }else{
            String deptId = session.getAttribute("dept_id").toString();
            list=dao.getUsersForDept((page-1)*limit,limit,deptId);
            x=dao.getUserCountForDept(deptId);
        }
        for (user m:list) {
            js.put("username",m.getUsername());
            js.put("id",m.getId());
            js.put("role_name",m.getRole_name());
            js.put("role",m.getRole());
            js.put("phone",m.getPhone());
            js.put("email",m.getEmail());
            js.put("deptId",m.getDepartmentId());
            js.put("deptName",m.getDepartment_name());
            js.put("status",m.getStatus());
            arr.add(js);
        }

        js.put("count", x);
        js.put("code",0);
        js.put("data",arr);
        return js;
    }

    @Override
    public JSONObject getRole(int page, int limit) {
        JSONObject js=new JSONObject();
        JSONArray arr=new JSONArray();
        List<Role> list=dao.getRole((page-1)*limit,limit);
        for (Role r : list) {
            js.put("id",r.getId());
            js.put("title",r.getTitle());
            js.put("content",r.getContent());
            js.put("peoples",r.getPeoples());
            arr.add(js);
        }
        int x= dao.getRoleCount();
        js.put("count", x);
        js.put("code",0);
        js.put("data",arr);
        return js;
    }

    @Override
    public JSONArray getMenuList() {
        JSONObject js=new JSONObject();
        JSONArray arr=new JSONArray();
        List<MenuType> list=dao.getMenuList();

        for(MenuType m1:list){
            js.put("id",m1.getId());
            js.put("title",m1.getTitle());
            if(m1.getMenuTwoLists().size()>0){
                JSONObject js2=new JSONObject();
                JSONArray arr2=new JSONArray();
                for(MenuTwoList m2:m1.getMenuTwoLists()){
                    js2.put("id",m2.getId());
                    js2.put("title",m2.getTitle());

                    String QX="[{id:"+m2.getId()+"_add,title:'添加'},{id:"+m2.getId()+"_edit,title:'修改'},{id:"+m2.getId()+"_delete,title:'删除'}]";
                    String QX2="[{id:"+m2.getId()+"_add,title:'添加'},{id:"+m2.getId()+"_edit,title:'一级修改'},{id:"+m2.getId()+"_small,title:'二级修改'},{id:"+m2.getId()+"_delete,title:'删除'}]";
                    if(m2.getId()==16 && m2.getTitle().equals("工具台账")){
                        js2.put("children", JSONArray.fromObject(QX2));
                    }else{
                        js2.put("children", JSONArray.fromObject(QX));
                    }


                    arr2.add(js2);
                }
                js.put("children",arr2);
            }else {
                js.put("children","");
            }
            arr.add(js);
        }
        System.out.println(arr);
        return arr;
    }

    @Override
    public JSONObject editRole(String roleName, String roleCont, String id) {
        JSONObject js=new JSONObject();
        roleCont+=",9";
        int x= dao.updateRole(roleName,roleCont.replaceAll("\"",""),id);
        List<Integer> adminId=dao.getAdminForRole(id);
        if(adminId.size()>0){
            String roleList=roleCont.replaceAll("\"","");
            dao.updateRole2(adminId,roleList);
            js.put("code","suss");
        }else {
            js.put("code","suss");
        }
        return js;
    }
}
